//package com.lantu.common.utils;
//
//
//import io.jsonwebtoken.JwtBuilder;
//import io.jsonwebtoken.Jwts;
//import io.jsonwebtoken.SignatureAlgorithm;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Field;
//import java.util.HashMap;
//import java.util.Map;
//
//import java.util.Date;
//import java.util.UUID;
//
//@Component
//public class JwtUtil {
//    // 有效期
//    private static long time = 1000 * 60 * 60 * 24;
//    // 令牌密钥
//    private static String signature = "admin";
//
//    // 创建JWT
//    public static String createToken(Object obj) {
//        JwtBuilder jwtBuilder = Jwts.builder();
//        Map<String, Object> claims = new HashMap<>();
//
//        // 使用反射获取对象的属性并添加到claims中
//        Field[] fields = obj.getClass().getDeclaredFields();
//        for (Field field : fields) {
//            field.setAccessible(true);
//            try {
//                claims.put(field.getName(), field.get(obj));
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
//        }
//
//        String jwtToken = jwtBuilder
//                // header
//                .setHeaderParam("typ", "JWT")
//                .setHeaderParam("alg", "HS256")
//                // payload
//                .setClaims(claims)
//                .setSubject(obj.getClass().getSimpleName())
//                .setExpiration(new Date(System.currentTimeMillis() + time))
//                .setId(UUID.randomUUID().toString())
//                // signature
//                .signWith(SignatureAlgorithm.HS256, signature)
//                .compact();
//        return jwtToken;
//    }
//
//}


package com.lantu.common.utils;

import com.alibaba.fastjson2.JSON;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;
@Component
public class JwtUtil {
    //有效期
    private static final long JWT_EXPIRE = 30 * 600 * 1000L;
    //令牌密钥
    private static final String JWT_KEY = "123456";

    public String createToken(Object data) {
        //当前时间
        long currentTime = System.currentTimeMillis();
        //过期时间
        long expTime = currentTime + JWT_EXPIRE;
        //构建Jwt
        JwtBuilder builder = Jwts.builder()
                .setId(UUID.randomUUID()+"")
                .setSubject(JSON.toJSONString(data))
                .setIssuer("system")
                .setIssuedAt(new Date(currentTime))
                .signWith(SignatureAlgorithm.HS256, encodeSecret(JWT_KEY))//针对jwt再做一个加密处理
                .setExpiration(new Date(expTime));
        return builder.compact();
    }

    //反向解析Jwt
    private SecretKey encodeSecret(String key) {
        byte[] encodedKey = Base64.getEncoder().encode(key.getBytes());
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "HmacSHA256");
    }

    public Claims parseToken(String token) {
        Claims body = Jwts.parser()
                .setSigningKey(encodeSecret(JWT_KEY))
                .parseClaimsJws(token)
                .getBody();
        return body;
    }

    public <T> T parseToken(String token,Class<T> clazz){
        Claims body = Jwts.parser()
                .setSigningKey(encodeSecret(JWT_KEY))
                .parseClaimsJws(token)
                .getBody();
        return JSON.parseObject(body.getSubject(),clazz);
    }

}
